package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.constant.RequestResponseFlagEnum;
import com.hzhr.iot.collect.constant.SimTypeEnum;
import com.hzhr.iot.collect.constant.TransferDirEnum;
import com.hzhr.iot.collect.constant.entity.FunctionEntity;
import com.hzhr.iot.collect.constant.entity.constant.FunctionEntityConstant;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.CommandCountService;
import com.hzhr.iot.collect.service.helper.Device_IotDevice_RegistInfoServiceHelper;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice_registInfo.IotDevice_RegistInfoPutModel;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WTCU注册 处理器
 * @author 48310
 *
 */
@Component
public class MessageProcessor2 implements MessageProcessor {
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Global global;
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	CommandCountService commandCountService;
	@Autowired
	Device_IotDevice_RegistInfoServiceHelper device_iotDevice_registInfoServiceHelper;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String iotDeviceDeviceId = parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString();
		String iccid = null;
		if (parsedMessageObj.get(MessageFieldKeyEnum.ICCID) != null) {
			iccid = parsedMessageObj.get(MessageFieldKeyEnum.ICCID).toString();
		}
		String simType = null;
		if (parsedMessageObj.get(MessageFieldKeyEnum.CARD_TYPE) != null) {
			String simTypeIdForMessgae = parsedMessageObj.get(MessageFieldKeyEnum.CARD_TYPE).toString();
			SimTypeEnum simTypeEnum = SimTypeEnum.getEnumByIdForMessage(simTypeIdForMessgae);
			if (simTypeEnum != null) simType = simTypeEnum.getId() + "";
		}
		
		//查询物联网设备
		IotDeviceModel iotDeviceModel = null;
		String iotDeviceCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID));
		if (!StringUtils.isEmpty(iotDeviceCode)) {
			Map<String, String> header = new HashMap<>();
			header.put("token", global.getFirstToken());
			Map<String, Object> query = new HashMap<>();
			query.put("_show", "one");
			query.put("_equal_iotPlatformDeviceId", deviceId);
			iotDeviceModel = device_iotDeviceService.get(header, query).getModel();
		}
		if (iotDeviceModel == null) throw new Exception("物联网设备不存在");

		List<Map<MessageFieldKeyEnum, Object>> responseMsgList = new ArrayList<>();//响应报文列表

		if (iotDeviceModel.getCode() != null && iotDeviceModel.getCode().equals(iotDeviceDeviceId)) {
			//更新设备号、sim卡类型、iccids

			//暂时注释掉 防止注册后表具无最近数据
//			iotDeviceRecord.setSim(iccid);
//			iotDeviceRecord.setSimtype(simType);
//			device_IotdeviceService.putIotdevice(iotDeviceRecord);

			Map<MessageFieldKeyEnum, Object> respMsg = messageUtil.generateResponse(protocolId, parsedMessageObj, "0102");
			responseMsgList.add(respMsg);
		} else {
			//更新设备号、sim卡类型、iccids
			IotDevice_RegistInfoPutModel body = new IotDevice_RegistInfoPutModel();
			body.setCode(iotDeviceDeviceId);
			body.setSimNo(iccid);
			//body.setSimTypeCode(simType);
			device_iotDevice_registInfoServiceHelper.putByIotDeviceId(iotDeviceModel.getId(), body);

			//设置表具初始上报时间
			FunctionEntity function = FunctionEntityConstant.getFunctionEntityByProtocolIdAndFunctionCode(protocolId, "9015");
			Map<MessageFieldKeyEnum, Object> dataArea = new HashMap<>();
			String hour = "00";
			String min = HzhrStringUtils.FillZeroForLeft(RandomUtils.nextInt(30) + "", 2);
			String sec = HzhrStringUtils.FillZeroForLeft(RandomUtils.nextInt(60) + "", 2);
			dataArea.put(MessageFieldKeyEnum.WAKE_TIME, hour + min + sec);
			dataArea.put(MessageFieldKeyEnum.WAKE_SEC, "0");
			dataArea.put(MessageFieldKeyEnum.PEAK_DELAY_SEC, "0");
			Map<MessageFieldKeyEnum, Object> messageObj = messageUtil.generateMessageObj(protocolId, function.getFunctiontmpId(),
					TransferDirEnum.SERVER_BEGIN, RequestResponseFlagEnum.REQ, iotDeviceDeviceId, messageUtil.getNextMessageId(), dataArea);
			responseMsgList.add(messageObj);

			Map<MessageFieldKeyEnum, Object> respMsg = messageUtil.generateResponse(protocolId, parsedMessageObj, "0000");
			responseMsgList.add(respMsg);
		}

		return responseMsgList;
	}
}
